package com.lc.tgxm.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.lc.tgxm.R;
import com.lc.tgxm.adapter.HomeListAdapter;
import com.lc.tgxm.model.HomeListmodel;
import com.lc.tgxm.util.Log;
import com.zcx.helper.adapter.AppRecyclerAdapter;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;

/**
 * RecyclerViewActivity
 * Created by Lei on 2018/8/21.
 */
public class RecyclerViewActivity extends BaseActivity {
    private RecyclerView rv_main;
    private SwipeRefreshLayout srl;
    private HomeListAdapter homeListAdapter;// 主列表的Adapter
    private EndlessRecyclerOnScrollListener endlessRecyclerOnScrollListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContenViewAndBack(R.layout.activity_recycler, R.string.mylist);

        rv_main = (RecyclerView) findViewById(R.id.rv_main);
        srl = (SwipeRefreshLayout) findViewById(R.id.srl_main);
        srl.setColorSchemeColors(ContextCompat.getColor(context, R.color.blue));
        homeListAdapter = new HomeListAdapter(context) {
            @Override
            public void onItemClick(int position, HomeListmodel homeListmodel) {

            }
        };
        rv_main.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = (LinearLayoutManager)
                homeListAdapter.verticalLayoutManager(context);
        rv_main.setLayoutManager(linearLayoutManager);
        rv_main.setAdapter(homeListAdapter);
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                testData(1);
                srl.setRefreshing(false);
                homeListAdapter.notifyDataSetChanged();
                endlessRecyclerOnScrollListener.reset();
                UtilToast.show("Refresh Complete.");
            }
        });

        endlessRecyclerOnScrollListener =
                new EndlessRecyclerOnScrollListener(linearLayoutManager) {
                    @Override
                    public void onLoadMore(int currentPage) {
                        if (currentPage > 2) {
                            UtilToast.show("Load Complete.");
                        } else {
                            testData(2);
                            UtilToast.show("Load more.");
                        }
                    }
                };
        rv_main.addOnScrollListener(endlessRecyclerOnScrollListener);

    }

    private void testData(int type) {
        ArrayList<AppRecyclerAdapter.Item> arrayList = new ArrayList<>();
        arrayList.addAll(HomeListmodel.testData(6));

        if (type == 1) {
            homeListAdapter.setList(arrayList);
        } else {
            homeListAdapter.addList(arrayList);
        }

    }

    public abstract class EndlessRecyclerOnScrollListener extends
            RecyclerView.OnScrollListener {

        private int previousTotal = 0;
        private boolean loading = true;
        int firstVisibleItem, visibleItemCount, totalItemCount;

        private int currentPage = 1;

        private LinearLayoutManager mLinearLayoutManager;

        public EndlessRecyclerOnScrollListener(
                LinearLayoutManager linearLayoutManager) {
            this.mLinearLayoutManager = linearLayoutManager;
        }

        public void reset() {
            previousTotal = 0;
            currentPage = 1;
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            visibleItemCount = recyclerView.getChildCount();
            totalItemCount = mLinearLayoutManager.getItemCount();
            firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
            Log.i(TAG, "totalItemCount", totalItemCount);
            Log.e(TAG, "previousTotal", previousTotal);
            Log.w(TAG, "visibleItemCount", visibleItemCount);

            if (loading) {
                if (totalItemCount > previousTotal) {
                    loading = false;
                    previousTotal = totalItemCount;
                }
            }
            if (!loading
                    && (totalItemCount - visibleItemCount) <= firstVisibleItem) {
                currentPage++;
                onLoadMore(currentPage);
                loading = true;
            }
        }

        public abstract void onLoadMore(int currentPage);
    }

}
